package com.example.dllo.moondog.main;

import android.graphics.Color;
import android.os.Handler;
import android.os.Message;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.KeyEvent;

import com.example.dllo.moondog.R;
import com.example.dllo.moondog.base.BaseActivity;
import com.example.dllo.moondog.base.BaseApplication;
import com.example.dllo.moondog.chat.ChatFragment;
import com.example.dllo.moondog.community.CommunityFragment;
import com.example.dllo.moondog.information.InformationFragment;
import com.example.dllo.moondog.tools.NoScrollViewPager;
import com.example.dllo.moondog.contact.ContactFragment;

import java.util.ArrayList;

public class MainActivity extends BaseActivity {

    private NoScrollViewPager noScrollViewPager;
    private TabLayout tabLayout;
    private ArrayList<Fragment> data;
    private MyViewPagerAdapter adapter;
    private static boolean isExit = false;

    Handler myHandler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message msg) {
            isExit = false;
            return false;
        }
    });

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        noScrollViewPager = (NoScrollViewPager) findViewById(R.id.viewpager_activity_main);
        tabLayout = (TabLayout) findViewById(R.id.tablayout_activity_main);

        data = new ArrayList<Fragment>();
        data.add(new InformationFragment());
        data.add(new ChatFragment());
        data.add(new ContactFragment());
        data.add(new CommunityFragment());

        adapter = new MyViewPagerAdapter(getSupportFragmentManager(),data,this);
        noScrollViewPager.setAdapter(adapter);
        tabLayout.setupWithViewPager(noScrollViewPager);
        tabLayout.setSelectedTabIndicatorColor(Color.parseColor("#25282e"));

        for (int i = 0; i < tabLayout.getTabCount(); i++) {
            TabLayout.Tab tab = tabLayout.getTabAt(i);
            if (tab != null) {
                tab.setCustomView(adapter.getTabView(i));
            }
        }


    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            exit();
            return false;
        }
        return super.onKeyDown(keyCode, event);
    }

    private void exit() {
        if (!isExit) {
            isExit = true;
//            showToast(String.valueOf(R.string.again_back));
            showToast(this.getString(R.string.again_back));
        } else {
            BaseApplication.finishAll();
            System.exit(0);
        }
    }
}
